//
//  CeShiChengViewController.m
//  u76ho
//
//  Created by 晔 邓 on 16/4/10.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "ChengZhangViewController.h"
#import "CollectionViewCell.h"
#import "ChengZhangCollectionViewCell.h"
#import "BoostViewController.h"



@interface ChengZhangViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>


{
    
    CompanyDetailModel * myModel;//请求数据


    NSMutableArray * RenQiZhiXingArray;//人气之星数组
    
    NSMutableArray * ChengZhangArray;//成长经历数组
    


}
@property (nonatomic, retain) UICollectionView * collection;

@property (nonatomic, retain) NSArray * headerArr;//区头视图

@end

static int fenyePage;


@implementation ChengZhangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //注册时间通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(caledarNoti:) name:@"caledarNoti" object:nil];
    
    RenQiZhiXingArray = [[NSMutableArray alloc]init];
    ChengZhangArray = [[NSMutableArray alloc]init];
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UICollectionViewFlowLayout*flow=[[UICollectionViewFlowLayout alloc]init];
    //列距
    flow.minimumInteritemSpacing=10;
    
    //行距
    flow.minimumLineSpacing=10;
    
    //滚动方向
    flow.scrollDirection= UICollectionViewScrollDirectionVertical;
    
    ;
    
    //区头大小
    flow.headerReferenceSize=CGSizeMake(0, 45);
    
    
    
    _collection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-200) collectionViewLayout:flow];
    _collection.backgroundColor=[UIColor whiteColor];

    _collection.tag = 1;
    //设置代理
    _collection.delegate = self;
    _collection.dataSource = self;
    
    
    
    _collection.showsVerticalScrollIndicator = YES;
    
    
      [_collection registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell"];
    _headerArr = @[@"q",@"w"];
    
    for (NSString * choose in _headerArr) {
        [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:choose];
    }
    
    
    [_collection registerNib:[UINib nibWithNibName:@"ChengZhangCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ChengZhangCollectionViewCell"];
       
       
    
    [self.view addSubview:_collection];
    
    
    __weak typeof(self)weakSelf = self;
    
    fenyePage = 1;
    
    
    
    _collection.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf requestDataWithDate:@"" andPage:[NSString stringWithFormat:@"%d",++fenyePage]];
    }];
    
    
    
    [self requestDataWithDate:@"" andPage:@"1"];
    
    

}

#pragma mark请求网络数据
-(void)requestDataWithDate:(NSString *)date andPage:(NSString *)page
{
    
    
    NSString * Pnum;
    
    if ([date isEqualToString:@""]) {
        Pnum = @"12";

        
    }else{
        Pnum = @"200";
    
    }

    
    [U76HONet CompanyChengZhangAction:@"" code:_yqCode ucode:[Singleton share].code withPage:page withPnum:Pnum withDates:date andDataBlock:^(id obj) {
        
        if ([obj isKindOfClass:[CompanyDetailModel class]]) {
            myModel = obj;
            
            //如果时间不为空
            if ([date isEqualToString:@""]) {
                
                
                if (RenQiZhiXingArray.count ==0) {
                    
                    [RenQiZhiXingArray addObjectsFromArray: myModel.renqiArray];
                }
                
                [ChengZhangArray addObjectsFromArray:myModel.waterPullArray];
                
                
                if (myModel.waterPullArray.count ==0) {
                    
                    [_collection.mj_footer endRefreshingWithNoMoreData];
                }else{
                    
                    
                    [_collection.mj_footer endRefreshing];
                    
                }
                
                
                
                
                //时间不为空
            }else{
                
                
                if (RenQiZhiXingArray.count ==0) {
                    
                    [RenQiZhiXingArray addObjectsFromArray: myModel.renqiArray];
                }
                
                if (ChengZhangArray.count!=0) {
                    
                    [ChengZhangArray removeAllObjects];
                }
                
                [ChengZhangArray addObjectsFromArray:myModel.waterPullArray];
                
                
                
                [_collection.mj_footer endRefreshingWithNoMoreData];
                
                
            }
            
            //回主线程 刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                
               // [_collection reloadSections:[NSIndexSet indexSetWithIndex:1]];

                //刷新数据
               [_collection reloadData];
                

            });

            
            
          
            

            
        }else{
            
            if (ChengZhangArray.count !=0) {
                [ChengZhangArray removeAllObjects];
            }
            
             [_collection.mj_footer endRefreshingWithNoMoreData];
            
            //回主线程刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                
                // [_collection reloadSections:[NSIndexSet indexSetWithIndex:1]];
                //刷新数据
                [_collection reloadData];
                
            });
            

            
        
        }        
        
        
        
    }];
    
    
}
#pragma mark - 时间消息通知
-(void)caledarNoti:(NSNotification *)noti
{
    NSLog(@"发现的通知接收到的消息%@",noti.object);
    
    NSArray * arr = noti.object;
    NSMutableArray * mArr = [[NSMutableArray alloc]init];
    [mArr addObject:arr.firstObject];
    if ([[[arr objectAtIndex:1] stringValue] length]  == 1) {
        NSString *str = [NSString stringWithFormat:@"0%@",[arr objectAtIndex:1]];
        [mArr addObject:str];
    }
    else
    {
        [mArr addObject:[arr objectAtIndex:1]];
    }
    
    if ([[arr.lastObject stringValue] length] == 1) {
        NSString *str = [NSString stringWithFormat:@"0%@",[arr objectAtIndex:2]];
        [mArr addObject:str];
    }
    else
    {
        [mArr addObject:[arr objectAtIndex:2]];
    }
    
    NSString * timeStr = [mArr componentsJoinedByString:@""];
    NSLog(@"timeStr is %@",timeStr);
    [self requestDataWithDate:timeStr andPage:@"1"];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 2;
}
#pragma mark  cell点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section ==1) {
        
        
        CompanyDetailWaterPullModel * model =ChengZhangArray[indexPath.row];
        
        
        if (![model.pic isEqualToString:@""]) {
            NSArray * imgUrlArr = [model.pic componentsSeparatedByString:@"|"];
            BoostViewController * boostVC = [[BoostViewController alloc]init];
            boostVC.imgArr = imgUrlArr;
            boostVC.contentStr = model.content;
            boostVC.i = 1;
            [self.navigationController pushViewController:boostVC animated:YES];
        }

    }
    

}


#pragma 自定义cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==0) {
        
        
        CompanyDetailRenqiModel * model = RenQiZhiXingArray[indexPath.row];
        
        ChengZhangCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChengZhangCollectionViewCell" forIndexPath:indexPath];
//        [cell.PersonHeaderImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,model.headpic]]];
      
        [cell.PersonHeaderImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,model.headpic]] placeholderImage:[UIImage imageNamed:@"公司简介成长－默认头像"]];
        cell.clipsToBounds = YES;

        cell.NameLabel.text = model.username;
        
        cell.MingXingLabel.text = model.name;

        
//       cell.backgroundColor = [UIColor redColor];
        
        return cell;
        
    }
   else {
        
    
    CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
       
       
       if (ChengZhangArray.count!=0) {
           CompanyDetailWaterPullModel * model = ChengZhangArray[indexPath.row];
           cell.watermodel = model;
       }
       cell.clipsToBounds = YES;

    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 5;
    cell.layer.borderWidth = 1;
    
    cell.layer.borderColor = [UIColorFromRGB(0xdddddd) CGColor];
    
    return cell;
        
    }
}


#pragma mark --区头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    
    
    //区头父视图
    UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:_headerArr[indexPath.section] forIndexPath:indexPath];
    
    //去上方汇线
    UIImageView * topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
    
    topImageView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
    
    // imageView.backgroundColor = [UIColor greenColor];
    [headerView addSubview:topImageView];

    
    
    
    //去显示图片
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 16, 15, 15)];
    
    // imageView.backgroundColor = [UIColor greenColor];
    [headerView addSubview:imageView];
    
    //区上面文字
    UILabel * nameLab = [[UILabel alloc]initWithFrame:CGRectMake(35, 13, 100, headerView.height-25)];
    
    nameLab.font = [UIFont systemFontOfSize:15];
    nameLab.textColor = [UIColor colorWithRed:0.76 green:0.84 blue:0.57 alpha:1.00];
    
    
    [headerView addSubview:nameLab];
    
    if (indexPath.section == 0) {
        nameLab.text = @"人气之星";
        imageView.image = [UIImage imageNamed:@"00a"];
        
    }else if(indexPath.section ==1){
        nameLab.text = @"成长经历";
        
        imageView.image = [UIImage imageNamed:@"00b"];
        
        
                UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //第二分区，右边日历视图
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 10, 15)];
        imageView.image = [UIImage imageNamed:@"00c"];
        
        [btn addSubview:imageView];
        
        
                btn.frame = CGRectMake(SCREEN_WIDTH-60, 10, 60, 20);
        
                //[btn setImage:[UIImage imageNamed:@"00c"] forState:UIControlStateNormal];
                //[btn setTitle:@"时间轨迹" forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(timeGJAction:) forControlEvents:UIControlEventTouchUpInside];
                [btn setTitleColor:UIColorFromRGB(0x6EC844) forState:UIControlStateNormal];
                
                [headerView addSubview:btn];
        

  
    }
    
    //区底部分割线
    UIImageView * bottomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0 , headerView.height-5 , headerView.width , 1)];
    bottomImageView.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00];
    
    // imageView.backgroundColor = [UIColor greenColor];
    [headerView addSubview:bottomImageView];
    

    
       // headerView.backgroundColor = [UIColor greenColor];
        return headerView;
    
}

#pragma mark --去日历
-(void)timeGJAction:(UIButton *)btn
{
    [Singleton alertCalendar];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    if (section ==0) {
        return RenQiZhiXingArray.count;
    }else{
        return ChengZhangArray.count;
    
    }

}
//设置cell大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section ==0) {
        return CGSizeMake(self.view.frame.size.width / 3 - 10, self.view.frame.size.width / 3+30);
    }else{
    
    return CGSizeMake(self.view.frame.size.width / 3 - 10, self.view.frame.size.width / 3 + 20);
    }
}
//设置区内四边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}


// 设置最小行间距，也就是前一行与后一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}

// 设置最小列间距，也就是左行与右一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark ---退出界面移除通知
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"销毁了*************************");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
